<!-- 擅长领域 -->

<template>
  <div class="container">
    <!-- 路径导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>师资管理</el-breadcrumb-item>
      <el-breadcrumb-item>擅长领域管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索区域 -->
    <el-row>
      <el-form :inline="true">
        <el-form-item label="擅长领域名称">
          <el-input v-model="queryInfo.name" clearable @clear="queryList" placeholder="请输入擅长领域名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="queryList">搜索</el-button>
          <el-button type="success" icon="el-icon-plus" @click="toAddArticle">新增擅长领域</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <!-- 表格区域 -->
    <el-table :data="noticeList" border style="width: 70%" :header-cell-style="{'text-align':'center'}"
      :cell-style="{'text-align':'center'}">
      <el-table-column width="100px" prop="id" label="ID"></el-table-column>
      <el-table-column prop="name" label="擅长领域名称"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button icon="el-icon-edit" type="primary" size="mini" @click="toEditArticle(scope.row)">编辑</el-button>
          <el-button icon="el-icon-delete" type="danger" size="mini" @click="deleteNotice(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="queryInfo.pageNum" :page-sizes="[1, 5, 10, 15, 20]" :page-size="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>

    <!-- 新增和修改的对话框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
      <el-form :model="article">
        <el-form-item label="擅长领域名称" label-width="120px">
          <el-input placeholder="请输入擅长领域名称" v-model="article.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="addNotice">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      url: "",
      // 查询信息对象
      queryInfo: {
        name: "",
        pageNum: 1,
        pageSize: 10,
      },
      total: 183,
      noticeList: [],
      title: "", // 对话框标题
      dialogFormVisible: false, // 是否显示对话框
      article: {
        // 文章对象
        id: "",
        name: "",
      },
      dialogVisible: false,
    };
  },
  methods: {
    // 删除文章
    deleteNotice(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http.delete("specialty/" + id).then((resp) => {
            if (resp.data.status == 200) {
              this.$message.success(resp.data.message);
              this.queryList();
            } else {
              this.$message.error(resp.data.message);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 进入添加的对话框
    toAddArticle() {
      this.title = "新增擅长领域";
      this.dialogFormVisible = true;
      // 把当前登录的用户的id赋值为文章的teacher_id
      // this.article.teacherId = sessionStorage.getItem("id");
    },
    // 进入修改的预览数据页面
    toEditArticle(row) {
      // console.log(row)
      // 赋值
      this.article = row;
      // 设置标题
      this.title = "修改擅长领域";
      // 开启对话框
      this.dialogFormVisible = true;
      // 动态的获取图片的名称并帮到到fileList
      // this.fileList = [
      //   {
      //     name: row.tabularChart,
      //     url: url + "downLoad?filename=" + row.tabularChart,
      //   },
      // ];
    },
    // 添加文章
    addNotice() {
      if (this.article.id) {
        // console.log(this.article);

        // 有值为true  修改
        this.$http.put("specialty", this.article).then((resp) => {
          if (resp.data.status == 200) {
            // 消息提示
            this.$message.success("修改成功！");
            // 清空数据
            this.article = {
              // 文章对象
              // 文章对象
              id: "",
              name: "",
              // publicityText: "",
              // createTime: "",
            };
            // 关闭对话框
            this.dialogFormVisible = false;
            // 重新查询
            this.queryList();
          } else {
            this.$message.error("接口异常！");
          }
        });
      } else {
        this.$http.post("specialty", this.article).then((resp) => {
          if (resp.data.status == 200) {
            // 消息提示
            this.$message.success("添加成功！");
            // 清空数据
            this.article = {
              // 文章对象
              // 文章对象
              id: "",
              name: "",
              // publicityText: "",
              // createTime: "",
            };
            // 关闭对话框
            this.dialogFormVisible = false;
            // 重新查询
            this.queryList();
          } else {
            this.$message.error("接口异常！");
          }
        });
      }
      //  在取消函数，添加成功， 修改成功的函数中加入以下代码实现图片的清空
      // this.fileList = [];
    },
    cancle() {
      this.dialogFormVisible = false;
      this.article = {
        // 文章对象
        // 文章对象
        id: "",
        name: "",
        // publicityText: "",
        // createTime: "",
      };
    },
    queryList() {
      this.$http
        .get("specialty/findByPage", {
          params: this.queryInfo,
        })
        .then((res) => {
          if (res.data.status == 200) {
            this.noticeList = res.data.data.list;
            this.total = res.data.data.total;
          } else {
            this.$message.error("查询失败!");
          }
        });
    },
    handleSizeChange(size) {
      // console.log('每页显示数量发生改变');
      this.queryInfo.pageSize = size;
      this.queryInfo.pageNum = 1;
      this.queryList();
    },
    handleCurrentChange(num) {
      // console.log('页码发生改变');
      this.queryInfo.pageNum = num;
      this.queryList();
    },
  },
  mounted() {
    this.queryList();
    // 动态的获取tool.js中的url，并赋值给当前组件的全局url
    // this.url = url;
  },
};
</script>
<style>
.el-breadcrumb {
  margin-bottom: 15px;
  padding: 0 0 15px;
  border-bottom: 1px solid #ddd;
}

.el-pagination {
  margin-top: 15px;
}

.el-link {
  padding: 0 5px;
}

/**设置富文本编辑器的最小高度，注意style中不能使用scoped **/
.ql-container {
  min-height: 200px;
}
</style>